package com.woniu.auth.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.auth.dto.UserDTO;
import com.woniu.auth.entity.SysPermission;
import com.woniu.auth.entity.SysUser;
import com.woniu.auth.service.LoginRecorder;
import com.woniu.auth.service.SysPermissionService;
import com.woniu.auth.service.SysUserService;
import com.woniu.auth.utils.JWTUtils;
import com.woniu.auth.vo.UserVO;
import com.woniu.common.util.Result;
import com.woniu.common.util.ResultVO;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：lgy
 * @description：用户控制层
 * @date ：2022/12/6 14:02
 */
@RestController
@RequestMapping("users")
@Api(value = "用户接口", tags = "AuthApi")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private LoginRecorder<String, String> loginRecorder;

    /**
     * @Description 登陆方法
     * @Para a
     * @Retun a
     * @Author WangMr
     * @Date Create by 2022/12/10 17:50
     */
    @PostMapping("login")
    public ResultVO login(@RequestBody UserDTO user) {
        SysUser sysUser = sysUserService.selectByTelAndPwd(user);
        if (sysUser != null) {
            List<SysPermission> sysPermissions = permissionService.selectPermissionByTel(user.getTel(), 0);
            String token = JWTUtils.createToken(user.getTel(), 3600);
            loginRecorder.put(token, "msg");
            Map map = new HashMap();
            map.put("sysPermissions", sysPermissions);
            map.put("token", token);
            map.put("user", sysUser);
            return ResultVO.success("登陆成功", map);
        } else {
            return ResultVO.failure("登陆失败，用户名或密码错误！！！");
        }
    }

    /**
     * token检查
     *
     * @param token
     * @return
     */
    @PostMapping("check")
    public ResultVO check(@RequestBody String token) {
        if (loginRecorder.get(token) == null) {
            return ResultVO.failure("无效token");
        }
        if (StringUtils.isEmpty(token)) {
            return ResultVO.failure("无效token");
        }
        return JWTUtils.verifyToken(token) ? ResultVO.success("有效token") : ResultVO.failure("无效token");
    }

    /**
     * 退出时把token删除
     *
     * @param request
     * @return
     */
    @DeleteMapping("logout")
    public ResultVO logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        //安全推出的时候从LoginRecored中移除
        loginRecorder.remove(token);
        return ResultVO.success("退出成功");
    }

    /**
     * @Description 查询全部的管理层带分页
     * @Parm a
     * @Retrn a
     * @Author WangMr
     * @Date Create by 2022/12/8 16:15
     */
    @PostMapping("queryAll")
    private Result findAllUser(@RequestBody UserDTO user) {
        PageHelper.startPage(user.getCurrentPage(), user.getPageSize());
        List<UserVO> userList = sysUserService.selectAllUser(user);
        PageInfo<UserVO> pageInfo = new PageInfo<UserVO>(userList);
        return Result.ok().data("userList",userList).data("pageInfo",pageInfo);
    }

    /**
     * @Description 改变用户状态（封禁/正常）
     * @Parm a
     * @Retrn a
     * @Author WangMr
     * @Date Create by 2022/12/10 17:48
     */
    @PutMapping("changePer")
    private Result changePer(@RequestBody UserDTO user) {
        sysUserService.updateByPrimaryKeySelective(new SysUser(user.getId(), null, null, null, null, null, user.getStatus()));
        return Result.ok();
    }

    /**
     * @Description 添加用户
     * @Para a
     * @Retun a
     * @Author WangMr
     * @Date Create by 2022/12/10 17:48
     */
    @PostMapping("add")
    private Result addUser(@RequestBody SysUser user) {
        SysUser sysUser = sysUserService.selectByTel(user);
        if (sysUser == null) {
            int i = sysUserService.insertSelective(user);
            return i == 1 ? Result.ok("添加成功！") : Result.error("添加失败！");
        } else {
            return Result.error("账户已存在");
        }
    }
}
